In [2]:
from pymatgen import MPRester, Composition
import re
import pprint
# Make sure that you have the Materials API key. Put the key in the call to
# MPRester if needed, e.g, MPRester("MY_API_KEY")
mpr = MPRester()
In [3]:
comp = Composition("Fe2O3")
anon_formula = comp.anonymized_formula
# We need to convert the formula to the dict form used in the database.
anon_formula = {m.group(1): int(m.group(2))
for m in re.finditer(r"([A-Z]+)(\d+)", anon_formula)}
data = mpr.query({"anonymous_formula": anon_formula},
properties=["task_id", "pretty_formula", "structure"])
print(len(data)) #Should show ~600 data.
In [4]:
# data now contains a list of dict. This shows you what each dict has.
# Note that the mp id is named "task_id" in the database itself.
pprint.pprint(data[0])
In [5]:
bs = mpr.get_bandstructure_by_material_id("mp-20470")
In [6]:
from pymatgen.electronic_structure.plotter import BSPlotter
%matplotlib inline
In [7]:
plotter = BSPlotter(bs)
plotter.show()
In [8]:
elastic_data = mpr.query({"elasticity": {"$exists": True}},
properties=["task_id", "pretty_formula", "elasticity"])
In [9]:
print(len(elastic_data))
pprint.pprint(elastic_data[0])
In general, almost any data can be obtained from MP using the MPRester, either via the high-level functions or the very powerful "query" method.
For more complex queries, you can refer to the documentation for the Materials API at https://github.com/materialsproject/mapidoc.
In [10]:
from pymatgen.analysis.structure_matcher import StructureMatcher
In [11]:
m = StructureMatcher() # You can customize tolerances etc., but the defaults usually work fine.
In [14]:
s1 = data[0]["structure"]
print(s1)
s2 = s1.copy()
s2.apply_strain(0.1)
print(s2)
In [15]:
print(m.fit(s1, s2))
For something more challenging, let's see how many structures are similar to Gd2O3
In [16]:
matches = []
for d in data:
if m.fit_anonymous(d["structure"], s1):
matches.append(d)
In [17]:
# The above fitting took a few seconds. We have 32 similar structures.
print(len(matches))
In [18]:
# Let's see a few of the matches.
pprint.pprint(matches[0])
pprint.pprint(matches[1])
pprint.pprint(matches[2])
You can see that we have successfully found iso-structural materials!
In [ ]: